package com.mouse.customview;


import com.ze.familyday.familyphotoframesquare.R;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.res.Resources;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;


/**
 * 自定义的dialog类，提供与alertdialog一样的方法
 * 
 * @author liheng
 * @version 1.0
 *
 */
public class CustomDialog extends Dialog {

	public CustomDialog(Context context, int theme) {
		super(context, theme);
		// TODO Auto-generated constructor stub
		
	}

	public CustomDialog(Context context) {
		super(context);
		
	}
	
	public static class Builder {
		private Context context;
		private String title;
		private String message;
		private String positiveButtonText;
		private String negativeButtonText;
		private View contentView;
		
		private TextView mTvMessage;

		private DialogInterface.OnClickListener positiveButtonClickListener,
				negativeButtonClickListener, customButtonClickListener;

		public Builder(Context context) {
			super();
			this.context = context;
		}

		public Builder setMessage(String message) {
			this.message = message;
			return this;
		}

		public Builder setMessage(int message) {
			this.message = (String) context.getText(message);
			return this;
		}
		
		public void setTvMessage(String message){
			if (this.mTvMessage == null) {
				return;
			}
			this.mTvMessage.setText(message);
		}

		public Builder setTitle(int title) {
			this.title = (String) context.getText(title);
			return this;

		}

		public Builder setTitle(String title) {
			this.title = title;
			return this;
		}

		public Builder setView(View v) {
			this.contentView = v;
			return this;
		}

		public Builder setPositiveButton(int positiveButtonText,
				DialogInterface.OnClickListener listener) {
			this.positiveButtonText = (String) context
					.getText(positiveButtonText);
			this.positiveButtonClickListener = listener;
			return this;
		}

		public Builder setPositiveButton(String positiveButtonText,
				DialogInterface.OnClickListener listener) {
			this.positiveButtonText = positiveButtonText;
			this.positiveButtonClickListener = listener;
			return this;
		}

		public Builder setNegativeButton(int negativeButtonText,
				DialogInterface.OnClickListener listener) {
			this.negativeButtonText = (String) context
					.getText(negativeButtonText);
			this.negativeButtonClickListener = listener;
			return this;
		}

		public Builder setNegativeButton(String negativeButtonText,
				DialogInterface.OnClickListener listener) {
			this.negativeButtonText = negativeButtonText;
			this.negativeButtonClickListener = listener;
			return this;
		}
		
		public Builder setCustomButton(DialogInterface.OnClickListener listener) {
			this.customButtonClickListener = listener;
			return this;
		}

		/**
		 * Create the custom dialog
		 */
		public CustomDialog create(int type) {
			LayoutInflater inflater = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			
			// instantiate the dialog with the custom Theme
			final CustomDialog dialog = new CustomDialog(context,
					R.style.customDialog);
			View layout = null;
			switch (type) {
			case 0:
				layout = inflater.inflate(R.layout.dialog_notice_layout, null);
				dialog.addContentView(layout, new LayoutParams(
						LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
				break;
			case 1:
				layout = inflater.inflate(R.layout.define_progress_dialog, null);
				dialog.addContentView(layout, new LayoutParams(
						LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
				((ImageButton) layout.findViewById(R.id.ibCancel))
				.setOnClickListener(new View.OnClickListener() {
					public void onClick(View v) {
						dialog.dismiss();
						if (customButtonClickListener != null) {
							customButtonClickListener.onClick(dialog,
									DialogInterface.BUTTON1);
						}
					}
				});
				break;
			case 2:
				layout = inflater.inflate(R.layout.initial, null);
				dialog.addContentView(layout, new LayoutParams(
						LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
				break;
			case 3:
				layout = inflater.inflate(R.layout.dialog_notice_layout1, null);
				dialog.addContentView(layout, new LayoutParams(
						LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
				break;
			default:
				break;
			}
			
			
			// set the dialog title
			
			/*if(title != null){
				((TextView) layout.findViewById(R.id.tvTitle)).setText(title);
			}else{
				layout.findViewById(R.id.tvTitle).setVisibility(View.GONE);
			}*/
			
			// set the confirm button
			
			
			if (positiveButtonText != null) {
				((Button) layout.findViewById(R.id.btPositive))
						.setText(positiveButtonText);
				
				((Button) layout.findViewById(R.id.btPositive))
				.setOnClickListener(new View.OnClickListener() {
					public void onClick(View v) {
						dialog.dismiss();
						if (positiveButtonClickListener != null) {
							positiveButtonClickListener.onClick(dialog,
									DialogInterface.BUTTON_POSITIVE);
						}
					}
				});
			} else {
				// if no confirm button just set the visibility to GONE
				layout.findViewById(R.id.btPositive).setVisibility(
						View.GONE);
			}
			// set the cancel button
			if (negativeButtonText != null) {
				((Button) layout.findViewById(R.id.btNegative))
						.setText(negativeButtonText);
				
				((Button) layout.findViewById(R.id.btNegative))
				.setOnClickListener(new View.OnClickListener() {
					public void onClick(View v) {
						dialog.dismiss();
						if (negativeButtonClickListener != null) {
							negativeButtonClickListener.onClick(dialog,
									DialogInterface.BUTTON_NEGATIVE);
						}
					}
				});
				
			} else {
				// if no confirm button just set the visibility to GONE
				layout.findViewById(R.id.btNegative).setVisibility(
						View.GONE);
			}
			// set the content message
			mTvMessage = (TextView) layout.findViewById(R.id.tvMessage);
			if (message != null) {
				//((TextView) layout.findViewById(R.id.tvMessage)).setText(message);
				mTvMessage.setText(message);
			} else if (contentView != null) {
				// if no message set
				// add the contentView to the dialog body
				((LinearLayout) layout.findViewById(R.id.llContent))
						.removeAllViews();
				((LinearLayout) layout.findViewById(R.id.llContent)).addView(
						contentView, new LayoutParams(
								LayoutParams.FILL_PARENT,
								LayoutParams.FILL_PARENT));
			}
			dialog.setContentView(layout);
			return dialog;
		}

	}
}
